import { Archives } from './archive';
let _Archives = Archives;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import ArchiveMock from './mdp/arc/archiveMock';
 * ArchiveMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mdp/arc/archive/listKey').reply(config => {
      let params = config.params;
      let mockArchives = _Archives.filter(archive => {
      	if(params.id||params.id==''){ if( archive.id.indexOf(params.id)>=0) return true;}
      	if(params.categoryId||params.categoryId==''){ if( archive.categoryId.indexOf(params.categoryId)>=0) return true;}
      	if(params.tagNames||params.tagNames==''){ if( archive.tagNames.indexOf(params.tagNames)>=0) return true;}
      	if(params.deptid||params.deptid==''){ if( archive.deptid.indexOf(params.deptid)>=0) return true;}
      	if(params.userid||params.userid==''){ if( archive.userid.indexOf(params.userid)>=0) return true;}
      	if(params.isShare||params.isShare==''){ if( archive.isShare.indexOf(params.isShare)>=0) return true;}
      	if(params.url||params.url==''){ if( archive.url.indexOf(params.url)>=0) return true;}
      	if(params.archiveAbstract||params.archiveAbstract==''){ if( archive.archiveAbstract.indexOf(params.archiveAbstract)>=0) return true;}
      	if(params.archiveContext||params.archiveContext==''){ if( archive.archiveContext.indexOf(params.archiveContext)>=0) return true;}
      	if(params.archiveTitle||params.archiveTitle==''){ if( archive.archiveTitle.indexOf(params.archiveTitle)>=0) return true;}
      	if(params.archivingUserid||params.archivingUserid==''){ if( archive.archivingUserid.indexOf(params.archivingUserid)>=0) return true;}
      	if(params.isFromArchiving||params.isFromArchiving==''){ if( archive.isFromArchiving.indexOf(params.isFromArchiving)>=0) return true;}
      	if(params.archivingDate||params.archivingDate==''){ if( archive.archivingDate.indexOf(params.archivingDate)>=0) return true;}
      	if(params.createDate||params.createDate==''){ if( archive.createDate.indexOf(params.createDate)>=0) return true;}
      	if(params.authorUserid||params.authorUserid==''){ if( archive.authorUserid.indexOf(params.authorUserid)>=0) return true;}
      	if(params.authorName||params.authorName==''){ if( archive.authorName.indexOf(params.authorName)>=0) return true;}
      	if(params.canDel||params.canDel==''){ if( archive.canDel.indexOf(params.canDel)>=0) return true;}
      	if(params.canEdit||params.canEdit==''){ if( archive.canEdit.indexOf(params.canEdit)>=0) return true;}
      	if(params.canRead||params.canRead==''){ if( archive.canRead.indexOf(params.canRead)>=0) return true;}
      	if(params.bizKey||params.bizKey==''){ if( archive.bizKey.indexOf(params.bizKey)>=0) return true;}
      	if(params.encryptType||params.encryptType==''){ if( archive.encryptType.indexOf(params.encryptType)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockArchives
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mdp/arc/archive/list').reply(config => {
      let params = config.params;
      let mockArchives = _Archives.filter(archive => {
      	var isTrue=false;
      	if(params.id||params.id==''){if(archive.id.indexOf(params.id)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.categoryId||params.categoryId==''){if(archive.categoryId.indexOf(params.categoryId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.tagNames||params.tagNames==''){if(archive.tagNames.indexOf(params.tagNames)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.deptid||params.deptid==''){if(archive.deptid.indexOf(params.deptid)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.userid||params.userid==''){if(archive.userid.indexOf(params.userid)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.isShare||params.isShare==''){if(archive.isShare.indexOf(params.isShare)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.url||params.url==''){if(archive.url.indexOf(params.url)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.archiveAbstract||params.archiveAbstract==''){if(archive.archiveAbstract.indexOf(params.archiveAbstract)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.archiveContext||params.archiveContext==''){if(archive.archiveContext.indexOf(params.archiveContext)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.archiveTitle||params.archiveTitle==''){if(archive.archiveTitle.indexOf(params.archiveTitle)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.archivingUserid||params.archivingUserid==''){if(archive.archivingUserid.indexOf(params.archivingUserid)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.isFromArchiving||params.isFromArchiving==''){if(archive.isFromArchiving.indexOf(params.isFromArchiving)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.archivingDate||params.archivingDate==''){if(archive.archivingDate.indexOf(params.archivingDate)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.createDate||params.createDate==''){if(archive.createDate.indexOf(params.createDate)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.authorUserid||params.authorUserid==''){if(archive.authorUserid.indexOf(params.authorUserid)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.authorName||params.authorName==''){if(archive.authorName.indexOf(params.authorName)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.canDel||params.canDel==''){if(archive.canDel.indexOf(params.canDel)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.canEdit||params.canEdit==''){if(archive.canEdit.indexOf(params.canEdit)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.canRead||params.canRead==''){if(archive.canRead.indexOf(params.canRead)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.bizKey||params.bizKey==''){if(archive.bizKey.indexOf(params.bizKey)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.encryptType||params.encryptType==''){if(archive.encryptType.indexOf(params.encryptType)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockArchives
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockArchives.length;
      mockArchives = mockArchives.filter((archive, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockArchives,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mdp/arc/archive/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  id  } = params;
      _Archives = _Archives.filter(archive => archive.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mdp/arc/archive/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _Archives = _Archives.filter(archive => !params.includes(archive.id));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mdp/arc/archive/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { id,categoryId,tagNames,deptid,userid,isShare,url,archiveAbstract,archiveContext,archiveTitle,archivingUserid,isFromArchiving,archivingDate,createDate,authorUserid,authorName,canDel,canEdit,canRead,bizKey,encryptType } = params;
      _Archives.some(archive => {
        if (archive.id === id) {
			archive.id = id;
			archive.categoryId = categoryId;
			archive.tagNames = tagNames;
			archive.deptid = deptid;
			archive.userid = userid;
			archive.isShare = isShare;
			archive.url = url;
			archive.archiveAbstract = archiveAbstract;
			archive.archiveContext = archiveContext;
			archive.archiveTitle = archiveTitle;
			archive.archivingUserid = archivingUserid;
			archive.isFromArchiving = isFromArchiving;
			archive.archivingDate = archivingDate;
			archive.createDate = createDate;
			archive.authorUserid = authorUserid;
			archive.authorName = authorName;
			archive.canDel = canDel;
			archive.canEdit = canEdit;
			archive.canRead = canRead;
			archive.bizKey = bizKey;
			archive.encryptType = encryptType
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mdp/arc/archive/add').reply(config => {
      var params = JSON.parse( config.data );
      let { id,categoryId,tagNames,deptid,userid,isShare,url,archiveAbstract,archiveContext,archiveTitle,archivingUserid,isFromArchiving,archivingDate,createDate,authorUserid,authorName,canDel,canEdit,canRead,bizKey,encryptType } = params;
      _Archives.push({
		id:id,
		categoryId:categoryId,
		tagNames:tagNames,
		deptid:deptid,
		userid:userid,
		isShare:isShare,
		url:url,
		archiveAbstract:archiveAbstract,
		archiveContext:archiveContext,
		archiveTitle:archiveTitle,
		archivingUserid:archivingUserid,
		isFromArchiving:isFromArchiving,
		archivingDate:archivingDate,
		createDate:createDate,
		authorUserid:authorUserid,
		authorName:authorName,
		canDel:canDel,
		canEdit:canEdit,
		canRead:canRead,
		bizKey:bizKey,
		encryptType:encryptType
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};